package com.cgt.xiaotao.promotion.controller;

import com.cgt.common.domain.dto.PageDTO;
import com.cgt.common.utils.R;
import com.cgt.xiaotao.promotion.domain.dto.CouponDiscountDTO;
import com.cgt.xiaotao.promotion.domain.dto.OrderProductDTO;
import com.cgt.xiaotao.promotion.domain.dto.ProductRequestDTO;
import com.cgt.xiaotao.promotion.domain.query.UserCouponQuery;
import com.cgt.xiaotao.promotion.domain.vo.CouponVO;
import com.cgt.xiaotao.promotion.service.IUserCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;


/**
 * 用户领取优惠券的记录，是真正使用的优惠券信息 前端控制器
 */
@Api(tags = "用户优惠券相关接口")
@RestController
@RequestMapping("/user-coupons")
@RequiredArgsConstructor
public class UserCouponController {

    private final IUserCouponService userCouponService;

    @ApiOperation("领取优惠券接口")
    @PostMapping("/{couponId}/receive")
    public R<String> receiveCoupon(@PathVariable String couponId) {
        try {
            userCouponService.receiveCoupon(couponId);
            return R.ok("领取成功！");
        } catch (Exception e) {
            return R.fail("领取失败！");
        }
    }

    @ApiOperation("兑换码兑换优惠券接口")
    @PostMapping("/{code}/exchange")
    public R<String> exchangeCoupon(@PathVariable("code") String code) {
        try {
            userCouponService.exchangeCoupon(code);
            return R.ok("兑换成功！");
        } catch (Exception e) {
            return R.fail("兑换失败！");
        }
    }

    @ApiOperation("分页查询我的优惠券接口")
    @PostMapping("page")
    public R<PageDTO<CouponVO>> queryMyCouponPage(@RequestBody UserCouponQuery query) {
        try {
            PageDTO<CouponVO> couponVOPageDTO = userCouponService.queryMyCouponPage(query);
            return R.ok(couponVOPageDTO);
        } catch (Exception e) {
            return R.fail("查询我的优惠券列表失败！");
        }
    }

    @ApiOperation("查询我的优惠券可用方案")
    @PostMapping("/available")
    public R<List<CouponDiscountDTO>> findDiscountSolution(@RequestBody ProductRequestDTO request) {
        try {
            List<OrderProductDTO> products = request.getProducts();
            return R.ok(userCouponService.findDiscountSolution(products));
        } catch (Exception e) {
            return R.fail("查询我的优惠券可用方案失败！");
        }
    }

    @ApiOperation("根据优惠券ID列表和商品列表获取最大优惠金额")
    @PostMapping("/maxDiscount")
    public BigDecimal getMaxDiscountAmount(@RequestBody ProductRequestDTO request) {
        List<OrderProductDTO> products = request.getProducts();
        List<Long> couponIds = request.getCouponIds();
        return userCouponService.getMaxDiscountAmount(couponIds, products);

    }

    @ApiOperation("使用优惠券/删除优惠券")
    @DeleteMapping("/{couponId}/use")
    public Boolean deleteUserCoupon(@PathVariable String couponId) {
        Long id = Long.valueOf(couponId);
        return userCouponService.deleteUserCoupon(id);
    }


}
